﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DealInnerCommBusiness                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/5/19 15:38:34                        
*└────────────────────────────────────────────────┘
*/

using Anchor120V7.InnerComm.InnerCommModel;
using Data;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Business
{
    public class DealInnerCommBusiness
    {
        private static DealInnerCommBusiness m_Business = null;

        private DealInnerCommBusiness()
        {

        }

        public static DealInnerCommBusiness Business
        {
            get
            {
                if (m_Business == null)
                    m_Business = new DealInnerCommBusiness();
                return m_Business;
            }
        }

        /// <summary>
        /// 发送驶向现场数据
        /// </summary>
        /// <param name="info"></param>
        public void SendDisPatientInfo(string ZDCode, string ambCode, string taskCode, int stateId)
        {
            try
            {
                CarInfo carInfo = GetGPSInfo(ambCode);
                if (carInfo == null) {
                    carInfo = new CarInfo();
                }
                //string data = PatientResuorce.patientResuorce.GetZYDisPatientInfo(taskCode,carInfo);
                string data = PatientResuorce.patientResuorce.GetZYDisPatientInfo1(taskCode,carInfo, ZDCode, stateId);

                if (!string.IsNullOrEmpty(data))
                {
                    string ptResult = HttpService.PostResponse(AppConfig.DDJJUrl, data, AppConfig.appCode, AppConfig.sign);
                    RetDTO ret = JsonHelper.DeserializeObject<RetDTO>(ptResult);
                    if (ret.ack.code == "0")
                    {
                        EventUtility.OnShowMsg("推送驶向现场的调度信息成功", false);
                        LogUtility.Debug("DealInnerCommBusiness/SendDisPatientInfo", "推送调度急救数据成功" + ret);
                    }
                    else
                    {
                        EventUtility.OnShowMsg("推送驶向现场的调度急救数据失败，请查看日志", true);
                        LogUtility.Debug("DealInnerCommBusiness/SendDisPatientInfo", "状态码：" + ret.ack.code + ",   " + ret.ack.text);
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtility.Error("DealInnerCommBusiness/SendDisPatientInfo", "推送驶向现场的调度急救数据失败：" + ex.ToString());
            }


        }


        /// <summary>
        /// 离开现场推送数据
        /// </summary>
        /// <param name="taskCode"></param>
        public void SendLeaveSceneInfo(string ambCode, string taskCode, string zDCode, int stateId)
        {
            try
            {
                CarInfo carInfo=GetGPSInfo(ambCode);
                if (carInfo == null)
                {
                    carInfo = new CarInfo();
                }
                string data = PatientResuorce.patientResuorce.GetZYDisPatientInfo1(taskCode, carInfo, zDCode, stateId);

                if (!string.IsNullOrEmpty(data))
                {
                    string ptResult = HttpService.PostResponse(AppConfig.DDJJUrl, data, AppConfig.appCode, AppConfig.sign);
                    RetDTO ret = JsonHelper.DeserializeObject<RetDTO>(ptResult);
                    if (ret.ack.code == "0")
                    {
                        EventUtility.OnShowMsg("推送离开现场的调度信息成功", false);
                        LogUtility.Debug("DealInnerCommBusiness/SendLeaveSceneInfo", "推送离开现调度急救数据成功" + ret);
                    }
                    else
                    {
                        EventUtility.OnShowMsg("推送离开现场的调度急救数据失败，请查看日志", true);
                        LogUtility.Debug("DealInnerCommBusiness/SendLeaveSceneInfo", "状态码：" + ret.ack.code + ",   " + ret.ack.text);
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtility.Error("DealInnerCommBusiness/SendLeaveSceneInfo", "推送离开现场的调度急救数据失败：" + ex.ToString());
            }
        }

        /// <summary>
        /// 到达医院推送数据
        /// </summary>
        /// <param name="taskCode"></param>
        public void SendArriveHospInfo(string ambCode, string taskCode, string zDCode, int stateId)
        {
            try
            {
                CarInfo carInfo = GetGPSInfo(ambCode);
                if (carInfo == null)
                {
                    carInfo = new CarInfo();
                }
                string data = PatientResuorce.patientResuorce.GetZYDisPatientInfo1(taskCode, carInfo, zDCode, stateId);

                if (!string.IsNullOrEmpty(data))
                {
                    string ptResult = HttpService.PostResponse(AppConfig.DDJJUrl, data, AppConfig.appCode, AppConfig.sign);
                    RetDTO ret = JsonHelper.DeserializeObject<RetDTO>(ptResult);
                    if (ret.ack.code == "0")
                    {
                        EventUtility.OnShowMsg("推送到达医院的调度信息成功", false);
                        LogUtility.Debug("DealInnerCommBusiness/SendLeaveSceneInfo", "推送到达医院调度急救数据成功" + ret);
                    }
                    else
                    {
                        EventUtility.OnShowMsg("推送到达医院的调度急救数据失败，请查看日志", true);
                        LogUtility.Debug("DealInnerCommBusiness/SendLeaveSceneInfo", "状态码：" + ret.ack.code + ",   " + ret.ack.text);
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtility.Error("DealInnerCommBusiness/SendLeaveSceneInfo", "推送到达医院的调度急救数据失败：" + ex.ToString());
            }
        }

        /// <summary>
        /// 发送病人上车数据
        /// </summary>
        /// <param name="Info"></param>
        //public void SendGetCarInfo(OrderContent Info)
        //{
        //    DataTable dt = taskBLL.GetInfoByID(Info.AmbCode);
        //    if (dt.Rows.Count <= 0)
        //        return;
        //    GetCarInfo CarInfo = new GetCarInfo();
        //    CarInfo.FirstAidID = Info.TaskCode;
        //    CarInfo.PatientID = Info.PatientID == null ? "" : Info.PatientID;

        //    CarInfo.YJDDDate = GetDistance(Info.AmbCode, Info.TaskCode)[0] == null ? "" : GetDistance(Info.AmbCode, Info.TaskCode)[0];
        //    CarInfo.ToDistance = GetDistance(Info.AmbCode, Info.TaskCode)[1] == null ? "" : GetDistance(Info.AmbCode, Info.TaskCode)[1];
        //    CarInfo.IDCard = Info.IDCard == null ? "" : Info.IDCard;
        //    CarInfo.YBCard = Info.YBCard == null ? "" : Info.YBCard;
        //    CarInfo.OtherCard = Info.OtherCard == null ? "" : Info.OtherCard;
        //    CarInfo.Sex = Info.Sex == null ? "" : Info.Sex;
        //    CarInfo.Marriage = Info.Marriage == null ? "" : Info.Marriage;
        //    CarInfo.PatientName = Info.PatientName == null ? "" : Info.PatientName;
        //    CarInfo.Age = Info.Age == null ? "" : Info.Age;
        //    CarInfo.BirthDate = Info.BirthDate == null ? "" : Info.BirthDate;
        //    CarInfo.Territory = Info.Territory == null ? "" : Info.Territory;
        //    CarInfo.Idiom = Info.Idiom == null ? "" : Info.Idiom;
        //    CarInfo.Mater = Info.Mater == null ? "" : Info.Mater;
        //    CarInfo.PhoneNumber = Info.POCTDate == null ? "" : Info.POCTDate;
        //    CarInfo.ContactName = Info.ContactName == null ? "" : Info.ContactName;
        //    CarInfo.JWS = Info.JWS == null ? "" : Info.JWS;
        //    CarInfo.SSS = Info.SSS == null ? "" : Info.SSS;
        //    CarInfo.JZS = Info.JZS == null ? "" : Info.JZS;
        //    CarInfo.YYS = Info.YYS == null ? "" : Info.YYS;
        //    CarInfo.GMS = Info.GMS == null ? "" : Info.GMS;
        //    CarInfo.ZS = Info.ZS == null ? "" : Info.ZS;

        //    CarInfo.CarZZ = Info.CarZZ == null ? "" : Info.CarZZ;

        //    if (dt.Rows[0]["离开现场时刻"] != null)
        //        CarInfo.LeaveDate = String.Format("{0:yyyy-MM-dd HH:mm:ss}", dt.Rows[0]["离开现场时刻"]);

        //    if (String.IsNullOrEmpty(Info.PatientID))//表示车载心电
        //    {
        //        DataTable dtECG = myBLL.GetECG("0", Info.TaskCode);
        //        CarInfo.TW = Info.TW == null ? "" : Info.TW;
        //        if (dtECG.Rows.Count > 0)
        //        {
        //            //CarInfo.TW = dtECG.Rows[0]["体温"].ToString() == null ? "" : dtECG.Rows[0]["体温"].ToString();
        //            CarInfo.MB = dtECG.Rows[0]["脉搏"].ToString() == null ? "" : dtECG.Rows[0]["脉搏"].ToString();
        //            CarInfo.HX = dtECG.Rows[0]["呼吸频率"].ToString() == null ? "" : dtECG.Rows[0]["呼吸频率"].ToString();
        //            CarInfo.SZY = dtECG.Rows[0]["舒张压"].ToString() == null ? "" : dtECG.Rows[0]["舒张压"].ToString();
        //            CarInfo.SSY = dtECG.Rows[0]["收缩压"].ToString() == null ? "" : dtECG.Rows[0]["收缩压"].ToString();

        //            CarInfo.CarECG = ImageBase.ImgToBase64String(Image.FromFile(dtECG.Rows[0]["心电图"].ToString()));
        //            CarInfo.CarECGDate = string.Format("{0:yyyy-MM-dd HH:mm:ss}", dtECG.Rows[0]["做心电时间"]);
        //            CarInfo.CarECGUploadDate = "";
        //        }
        //        else
        //        {
        //            //CarInfo.TW = Info.TW == null ? "" : Info.TW;
        //            CarInfo.MB = Info.MB == null ? "" : Info.MB;
        //            CarInfo.HX = Info.HX == null ? "" : Info.HX;
        //            CarInfo.SZY = Info.SZY == null ? "" : Info.SZY;
        //            CarInfo.SSY = Info.SSY == null ? "" : Info.SSY;

        //            CarInfo.CarECG = "";
        //            CarInfo.CarECGDate = "";
        //            CarInfo.CarECGUploadDate = "";
        //        }

        //        CarInfo.XWFBDate = Info.XWFBDate == null ? "" : Info.XWFBDate;
        //        CarInfo.YLJCDate = Info.YLJCDate == null ? "" : Info.YLJCDate;
        //        CarInfo.POCTDate = Info.POCTDate == null ? "" : Info.POCTDate;
        //        CarInfo.YWDate = Info.YWDate == null ? "" : Info.YWDate;
        //    }

        //    if (!String.IsNullOrEmpty(Info.PatientID))
        //    {
        //        DataTable tmpDT = HosBLL.GetHospitalResourceOnTask("1", Info.TaskCode);
        //        if (tmpDT.Rows.Count > 0)
        //        {
        //            CarInfo.YLJGDM = tmpDT.Rows[0]["医院编码"].ToString();
        //            CarInfo.YLJGMC = tmpDT.Rows[0]["医院名称"].ToString();
        //        }
        //    }
        //    else
        //    {
        //        CarInfo.YLJGDM = "";
        //        CarInfo.YLJGMC = "";
        //    }


        //    CarInfo.CQZD = Info.CQZD == null ? "" : Info.CQZD;
        //    CarInfo.YYQK = Info.YYQK == null ? "" : Info.YYQK;
        //    CarInfo.CLNR = Info.CLNR == null ? "" : Info.CLNR;
        //    CarInfo.CarVideo = Info.CarVideo == null ? "" : Info.CarVideo;
        //    CarInfo.UserID = dt.Rows[0]["操作用户工号"].ToString();
        //    CarInfo.UserName = dt.Rows[0]["操作用户姓名"].ToString();
        //    CarInfo.UploadDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        //    string json = ObjectToJson.GetObjectToString(CarInfo);
        //    LogUtility.Debug("SendGetCarInfo", json);
        //    json = AES.AesEncrypt(json, AppConfig.Key);
        //    string str = WebServiceFactory.GetService().ReceiveCarLeavingInfo(json);
        //    ReturnMsg msg = new ReturnMsg();
        //    msg = ObjectToJson.CopyObjectProperty(msg, str);
        //    if (msg.Status == "0")
        //    {
        //        ShowMsg("病人上车数据上传成功,任务编码:" + CarInfo.FirstAidID, false);
        //        LogUtility.Debug("SendGetCarInfo", "病人上车数据上传成功,任务编码:" + CarInfo.FirstAidID);
        //    }
        //    else
        //    {
        //        ShowMsg("病人上车数据上传失败,任务编码:" + CarInfo.FirstAidID + ",错误原因:" + msg.ResMsg, true);
        //        LogUtility.Error("SendGetCarInfo", "病人上车数据上传失败,任务编码:" + CarInfo.FirstAidID + ",错误原因:" + msg.ResMsg);
        //    }
        //}





        /// <summary>
        /// 根据车辆获取任务号
        /// </summary>
        /// <param name="ambCode"></param>
        /// <returns></returns>
        public string GetTaskByID(string ambCode)
        {

            return DapperData.GetTaskByID(ambCode);

        }

        /// <summary>
        /// 获取GPS数据
        /// </summary>
        /// <param name="ambCode"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private CarInfo GetGPSInfo(string ambCode)
        {
            return DapperData.GetGPSInfo(ambCode);
        }

        /// <summary>
        /// 获取距离和时间
        /// </summary>
        /// <param name="AmbCode"></param>
        /// <param name="TaskCode"></param>
        /// <returns>0:时间 1:距离</returns>
        //public string[] GetDistance(string AmbCode, string TaskCode)
        //{
        //    string[] resultMsg = new string[2];
        //    DataTable HosInfo = HosBLL.GetHospitalResourceOnTask("1", TaskCode);
        //    if (HosInfo != null)
        //    {
        //        if (HosInfo.Rows.Count > 0)
        //        {
        //            double[] gps = gpsBLL.GetGpsByAmbCode(AmbCode, TaskCode);
        //            if (gps.Length > 0)
        //            {
        //                string startGPSX = HosInfo.Rows[0]["X"].ToString();
        //                string startGPSY = HosInfo.Rows[0]["Y"].ToString();

        //                string endGPSX = gps[0].ToString();
        //                string endGPSY = gps[1].ToString();

        //                string result = CommMethods.SendDataByGET(AppConfig.BDApi + "?output=json&origins=" + startGPSY + "," + startGPSX + "&destinations=" + endGPSY + "," + endGPSX + "&ak=B227b94dc1f3401cf0f240460ecb8bae" + "&coord_type=wgs84");
        //                if (!String.IsNullOrEmpty(result))
        //                {
        //                    BaiduResult baiduResult = new BaiduResult();
        //                    baiduResult = ObjectToJson.CopyObjectProperty(baiduResult, result);
        //                    if (baiduResult.message.Equals("成功"))
        //                    {
        //                        baiduResult.result[0].duration.text = baiduResult.result[0].duration.text.Substring(0, baiduResult.result[0].duration.text.Length - 2);
        //                        double mins = Convert.ToDouble(baiduResult.result[0].duration.text);
        //                        resultMsg[0] = DateTime.Now.AddMinutes(mins).ToString("yyyy-MM-dd HH:mm:ss");
        //                        resultMsg[1] = baiduResult.result[0].distance.text;
        //                    }
        //                    else
        //                    {
        //                        resultMsg[0] = "";
        //                        resultMsg[1] = "";
        //                    }
        //                }
        //                else
        //                {

        //                    resultMsg[0] = "";
        //                    resultMsg[1] = "";
        //                }
        //            }
        //        }
        //    }
        //    return resultMsg;
        //}




    }
}
